<template>
  <div class="list-table">
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('申请标题')" prop="title">
        <el-input
          v-model="queryParams.title"
          size="small"
          :placeholder="$t('申请标题')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('申请人')" prop="applyUserId">
        <UserSelect v-model="queryParams.applyUserId" />
      </el-form-item>
      <el-form-item :label="$t('流程名称')" prop="key">
        <el-select v-model="queryParams.key" size="small" :placeholder="$t('流程名称')" filterable @visible-change="$forceUpdate()">
          <el-option
            v-for="process in procDefKeyOptions"
            :key="process.key"
            :label="process.name"
            :value="process.key"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('申请时间')">
        <DatePicker v-model="dateRange" type="daterange" />
      </el-form-item>
      <el-form-item :label="$t('审批状态')" prop="actStatus">
        <dictSelect
          v-model="queryParams.actStatus"
          size="small"
          :clearable="true"
          dictionary="act_apply_status"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      tooltip-effect="darkTable"
      :data="applyList"
      border
      class="nowrapTable"
    >
      <el-table-column :label="$t('申请标题')" align="left" prop="title" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <el-link type="primary" @click="openTask(scope.row)">{{ scope.row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column :label="$t('申请人')" align="center" prop="applyNickName" class-name="small-padding fixed-width" width="150" />
      <el-table-column :label="$t('申请时间')" align="center" prop="beginTime" class-name="small-padding fixed-width" width="150" />
      <el-table-column :label="$t('结束时间')" align="center" prop="endTime" class-name="small-padding fixed-width" width="150" />
      <el-table-column :label="$t('流程名称')" align="center" prop="key" class-name="small-padding fixed-width" :formatter="processFormat" width="150" />
      <el-table-column :label="$t('审批状态')" align="center" prop="actStatus" width="120">
        <template slot-scope="scope">
          <ProcessStatus :status="scope.row.actStatus" :act-id="scope.row.id" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="250">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.actStatus === 'D'"
            size="small"
            type="text"
            icon="el-icon-pause-circle"
            @click="stop(scope.row)"
          >{{ $t('终止流程') }}</el-button>
          <el-button
            size="small"
            type="text"
            icon="el-icon-pause-circle"
            @click="restore(scope.row)"
          >{{ $t('恢复流程') }}</el-button>
          <el-button
            v-if="scope.row.actStatus === 'D'"
            size="small"
            type="text"
            icon="el-icon-pause-circle"
            @click="urge(scope.row)"
          >{{ $t('催办') }}</el-button>
          <el-button
            size="small"
            type="text"
            @click="ccProcess(scope.row)"
          >{{ $t('传阅') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :visible.sync="formOpen" :title="$t('任务详情')" custom-class="dialogForm" :close-on-click-modal="false" :lock-scroll="false">
      <ProcessInfoForm ref="processInfoForm" />
    </el-dialog>

    <el-dialog :visible.sync="ccProcessOpen" :title="$t('流程传阅')" :close-on-click-modal="false" width="30%">
      <el-form ref="ccForm" :model="ccForm" :inline="true">
        <user-select v-model="ccForm.ccUser" :multiple="true" clearable />
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelCcProcess">{{ $t('取消') }}</el-button>
        <el-button type="primary" @click="submitCcProcess">{{ $t('确定') }}</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { allList, listDataProcess, hastenWork, ccProcess, restore } from '@/api/act/process/actProcess'
import ProcessInfoForm from '../process/processInfoForm'
export default {
  components: {
    ProcessInfoForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 待办表格数据
      applyList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      formOpen: false,
      // 传阅用户弹出层是否展示
      ccProcessOpen: false,
      // 查询条件的时间区间
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: undefined,
        applyUserId: '',
        processName: undefined,
        actStatus: undefined
      },
      wfMenuProcessData: {},
      // 表单参数
      form: {},
      // 流程下拉框
      procDefKeyOptions: [],
      // 流程传阅表单
      ccForm: {
        actId: undefined,
        ccUser: []
      }
    }
  },
  created() {
    listDataProcess().then(response => {
      this.procDefKeyOptions = response.data
    })
    this.getList()
  },
  methods: {
    /** 查询待办列表 */
    getList() {
      this.loading = true
      allList(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.applyList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.formOpen = false
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.dateRange = []
      this.handleQuery()
    },
    // 打开流程详情页面
    openTask(row) {
      this.formOpen = true
      this.$nextTick(() => {
        row.actId = row.id
        this.$refs.processInfoForm.init(row)
      })
    },
    processFormat(row) {
      var actions = []
      Object.keys(this.procDefKeyOptions).map((key) => {
        if (this.procDefKeyOptions[key].key === ('' + row.key)) {
          actions.push(this.procDefKeyOptions[key].name)
          return false
        }
      })
      return actions.join('')
    },
    stop(data) {
      data.actId = data.id
      this.stopProcess(data)
    },
    urge(data) {
      this.$confirm(this.$t('是否再次给审批人发送提醒?'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(function() {
        return hastenWork(data.id)
      }).then(() => {
        this.msgSuccess(this.$t('提醒已发送'))
      }).catch(function() {
      })
    },
    // 抄送流程
    ccProcess(row) {
      this.ccProcessOpen = true
      this.ccForm.actId = row.id
      console.log(row)
    },
    // 恢复流程
    restore(data) {
      this.$confirm(this.$t('是否确认恢复流程(该功能用于恢复异常终止的流程)'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(() => {
        this.loading = true
      }).then(function() {
        return restore(data.id)
      }).then(() => {
        this.msgSuccess(this.$t('操作成功'))
        this.getList()
      }).catch(function() {
      })
    },
    // 关闭抄送页面
    cancelCcProcess() {
      this.ccProcessOpen = false
    },
    // 发送抄送
    submitCcProcess() {
      if (this.ccForm.ccUser.length !== 0) {
        ccProcess(this.ccForm).then(() => {
          this.msgSuccess(this.$t('传阅成功'))
          this.ccProcessOpen = false
        })
      } else {
        this.msgError(this.$t('请选择传阅用户'))
      }
    }
  }
}
</script>
